package tc.de.krawallmann.robobridge.visualization;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;
import javax.swing.JPanel;

import eu.hansolo.steelseries.extras.Led;
import eu.hansolo.steelseries.gauges.Linear;
import eu.hansolo.steelseries.gauges.Radial;
import eu.hansolo.steelseries.tools.BackgroundColor;
import eu.hansolo.steelseries.tools.BackgroundImageFactory;
import eu.hansolo.steelseries.tools.ColorDef;
import eu.hansolo.steelseries.tools.ForegroundType;
import eu.hansolo.steelseries.tools.FrameDesign;
import eu.hansolo.steelseries.tools.FrameEffect;
import eu.hansolo.steelseries.tools.KnobType;
import eu.hansolo.steelseries.tools.LcdColor;
import eu.hansolo.steelseries.tools.LedColor;
import eu.hansolo.steelseries.tools.NumberFormat;
import eu.hansolo.steelseries.tools.Section;
import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.implementation.RP6Base;
import tc.de.krawallmann.robobridge.util.BitOperations;

public class BaseFrame extends AbstractRoboViewDisplay {

	private Radial speedLeft;
	private Radial speedRight;
	private Linear battery;
	private Led bumperRight;
	private Led bumperLeft;
	private Led acsRight;
	private Led acsLeft;
	private Linear lightLeft;
	private Linear lightRight;
	private Led l1;
	private Led l2;
	private Led l3;
	private Led l4;
	private Led l5;
	private Led l6;
	private RP6Base base;
	
	public BaseFrame(RP6Base base) {
		super("RP6 Base @"+base.getDeviceId(), false, true, false, true);
		this.base = base;
        
        setLayout(null);
        
        FrameDesign design = FrameDesign.SHINY_METAL;
		speedLeft = new Radial();
		speedRight = new Radial();

		speedLeft.setForegroundType(ForegroundType.FG_TYPE1);
		speedLeft.setBackgroundColor(BackgroundColor.DARK_GRAY);
		speedLeft.setFrameDesign(design);
		speedLeft.setCustomLedColor(new java.awt.Color(0, 255, 0));
		speedLeft.setToolTipText("Speed of the robot's left track");
		speedLeft.setHighlightArea(true);
		speedLeft.setDoubleBuffered(true);
		speedLeft.setFocusable(false);
		speedLeft.setFrameEffect(FrameEffect.EFFECT_INNER_FRAME);
		speedLeft.setKnobType(KnobType.METAL_KNOB);
		speedLeft.setLcdColor(LcdColor.BLUE2_LCD);
		speedLeft.setLcdThreshold(150.0);
		speedLeft.setMajorTickSpacing(10.0);
		speedLeft.setMaxValue(250.0);
		speedLeft.setPeakValue(120.0);
		speedLeft.setThreshold(150.0);
		speedLeft.setThresholdVisible(true);
		speedLeft.setTitle("Speed left");
		speedLeft.setTrackStop(250.0);
		speedLeft.setUnitString("1/200ms");
		speedLeft.setUserLedColor(LedColor.GREEN_LED);
		speedLeft.setUserLedVisible(true);
		speedLeft.setUserLedOn(true);
		speedLeft.setSectionsVisible(true);
		speedLeft.setSection3DEffectVisible(true);
		speedLeft.setAreasVisible(true);
		//speedLeft.seta

		speedLeft.addSection(new Section(0, 120, Color.GREEN));
		speedLeft.addSection(new Section(120, 150, Color.ORANGE));
		speedLeft.addSection(new Section(150, 250, Color.RED));

		speedLeft.addArea(new Section(150, 250, Color.RED));
		speedLeft.addArea(new Section(120, 150, Color.ORANGE));
		speedLeft.setBounds(0, 0, 200, 200);
		add(speedLeft);

		speedRight.setForegroundType(ForegroundType.FG_TYPE1);
		speedRight.setBackgroundColor(BackgroundColor.DARK_GRAY);
		speedRight.setFrameDesign(design);
		speedRight.setToolTipText("Speed of the robot's right track");
		speedRight.setCustomLedColor(new java.awt.Color(0, 255, 0));
		speedRight.setHighlightArea(true);
		speedRight.setDoubleBuffered(true);
		speedRight.setFocusable(false);
		speedRight.setFrameEffect(FrameEffect.EFFECT_INNER_FRAME);
		speedRight.setKnobType(KnobType.METAL_KNOB);
		speedRight.setLabelNumberFormat(NumberFormat.STANDARD);
		speedRight.setLcdColor(LcdColor.BLUE2_LCD);
		speedRight.setLcdThreshold(150.0);
		speedRight.setMajorTickSpacing(10.0);
		speedRight.setMaxValue(250.0);
		speedRight.setPeakValue(120.0);
		speedRight.setThreshold(150.0);
		speedRight.setThresholdVisible(true);
		speedRight.setTitle("Speed right");
		speedRight.setTrackStop(250.0);
		speedRight.setUnitString("1/200ms");
		speedRight.setUserLedColor(LedColor.GREEN_LED);
		speedRight.setUserLedVisible(true);
		speedRight.setUserLedOn(true);

		speedRight.setSectionsVisible(true);
		speedRight.setSection3DEffectVisible(true);
		speedRight.setAreasVisible(true);
	//	speedRight.setArea3DEffectVisible(true);

		speedRight.addSection(new Section(0, 120, Color.GREEN));
		speedRight.addSection(new Section(120, 150, Color.ORANGE));
		speedRight.addSection(new Section(150, 250, Color.RED));

		speedRight.addArea(new Section(150, 250, Color.RED));
		speedRight.addArea(new Section(120, 150, Color.ORANGE));

		speedRight.setBounds(210, 0, 200, 200);
		add(speedRight);

		battery = new Linear();

		battery.setBackgroundColor(BackgroundColor.DARK_GRAY);
		battery.setFrameDesign(design);
		battery.setFrameEffect(FrameEffect.EFFECT_INNER_FRAME);
		battery.setLcdColor(LcdColor.BLUE2_LCD);
		battery.setLcdInfoString("Battery Voltage");
		battery.setLcdUnitString("V");
		battery.setLcdDecimals(2);
		battery.setLcdUnitStringVisible(true);
		battery.setMaxValue(12.0);
		battery.setThreshold(9.0);
		battery.setThresholdVisible(true);
		battery.setTitle("Battery [V]");
		battery.setUnitString("V");
		battery.setUnitStringVisible(false);
		battery.setValueColor(ColorDef.BLUE);

		battery.setSectionsVisible(true);
		battery.setAreasVisible(true);
		battery.addArea(new Section(0, 6, Color.RED));
		battery.addArea(new Section(6, 8, Color.GREEN));
		battery.addArea(new Section(8, 12, Color.RED));

		battery.setBounds(0, 220, 250, 100);
		add(battery);

		JPanel bumperPanel = new JPanel(new GridLayout(1, 2));
		bumperPanel.setOpaque(true);
		final JPanel brPanel = new JPanel(new BorderLayout());

		bumperRight = new Led();
		bumperRight.setLedColor(LedColor.RED);
		brPanel.add(BorderLayout.CENTER, bumperRight);
		JLabel brlabel=new JLabel("Bumper right", JLabel.CENTER);
		brlabel.setForeground(Color.WHITE);
		brPanel.add(BorderLayout.SOUTH, brlabel);
		brPanel.setOpaque(false);
		bumperPanel.add(brPanel);

		final JPanel blPanel = new JPanel(new BorderLayout());
		bumperLeft = new Led();
		bumperLeft.setLedColor(LedColor.RED);
		blPanel.add(BorderLayout.CENTER, bumperLeft);
		JLabel bllabel= new JLabel("Bumper left", JLabel.CENTER);
		bllabel.setForeground(Color.white);
		blPanel.add(BorderLayout.SOUTH, bllabel);
		blPanel.setOpaque(false);
		bumperPanel.add(blPanel);
		bumperPanel.setOpaque(false);
		bumperPanel.setBounds(270, 220, 150, 50);
		add(bumperPanel);

		JPanel acsPanel = new JPanel(new GridLayout(1, 2));
		final JPanel acsRightPanel = new JPanel(new BorderLayout());
		acsRight = new Led();
		acsRight.setLedColor(LedColor.RED);
		acsRightPanel.add(BorderLayout.CENTER, acsRight);
		JLabel arlabel = new JLabel("ACS right", JLabel.CENTER);
		arlabel.setForeground(Color.WHITE);
		acsRightPanel.add(BorderLayout.SOUTH, arlabel);
		acsRightPanel.setOpaque(false);
		acsPanel.add(acsRightPanel);

		final JPanel acsLeftPanel = new JPanel(new BorderLayout());
		acsLeft = new Led();
		acsLeft.setLedColor(LedColor.RED);
		acsLeftPanel.add(BorderLayout.CENTER, acsLeft);
		JLabel allabel = new JLabel("ACS left", JLabel.CENTER);
		allabel.setForeground(Color.WHITE);
		acsLeftPanel.add(BorderLayout.SOUTH, allabel);
		acsLeftPanel.setOpaque(false);
		acsPanel.add(acsLeftPanel);

		acsPanel.setBounds(270, 270, 150, 50);
		acsPanel.setOpaque(false);
		add(acsPanel);

		JPanel lightPanel = new JPanel(new GridLayout(2, 1));

		lightLeft = new Linear();
		lightRight = new Linear();

		lightLeft.setBackgroundColor(BackgroundColor.DARK_GRAY);
		lightLeft.setFrameDesign(design);
		lightLeft.setFrameEffect(FrameEffect.EFFECT_INNER_FRAME);
		lightLeft.setLcdColor(LcdColor.BLUE2_LCD);
		lightLeft.setLcdUnitStringVisible(true);
		lightLeft.setUserLedColor(LedColor.GREEN);
		lightLeft.setMaxValue(1024.0);
		lightLeft.setThreshold(600.0);
		lightLeft.setThresholdVisible(true);
		lightLeft.setUnitString(" ");
		lightLeft.setTickmarksVisible(false);
		lightLeft.setTitle("Light left");
		lightLeft.setUnitStringVisible(false);
		lightLeft.setValueColor(ColorDef.BLUE);
		lightLeft.setLedColor(LedColor.GREEN);

		lightRight.setBackgroundColor(BackgroundColor.DARK_GRAY);
		lightRight.setFrameDesign(design);
		lightRight.setFrameEffect(FrameEffect.EFFECT_INNER_FRAME);
		lightRight.setLcdColor(LcdColor.BLUE2_LCD);
		lightRight.setLcdUnitStringVisible(true);
		lightRight.setUserLedColor(LedColor.GREEN);
		lightRight.setMaxValue(1024.0);
		lightRight.setThreshold(600.0);
		lightRight.setUnitString(" ");
		lightRight.setThresholdVisible(true);
		lightRight.setTickmarksVisible(false);
		lightRight.setTitle("Light right");
		lightRight.setUnitStringVisible(false);
		lightRight.setValueColor(ColorDef.BLUE);
		lightRight.setLedColor(LedColor.GREEN);

		lightPanel.add(lightLeft);
		lightPanel.add(lightRight);
		lightPanel.setBounds(420, 0, 250, 250);
		lightPanel.setOpaque(false);
		add(lightPanel);

		JPanel LEDPanel = new JPanel(new GridLayout(2, 6));

		l1 = new Led();
		l1.setLedColor(LedColor.GREEN);
		LEDPanel.add(l1);
		l2 = new Led();
		l2.setLedColor(LedColor.RED);
		LEDPanel.add(l2);
		l3 = new Led();
		l3.setLedColor(LedColor.RED);
		LEDPanel.add(l3);
		l4 = new Led();
		l4.setLedColor(LedColor.GREEN);
		LEDPanel.add(l4);
		l5 = new Led();
		l5.setLedColor(LedColor.RED);
		LEDPanel.add(l5);
		l6 = new Led();
		l6.setLedColor(LedColor.RED);
		LEDPanel.add(l6);
		JLabel l1 = new JLabel("LED1", JLabel.CENTER);
		l1.setForeground(Color.white);
		LEDPanel.add(l1);
		JLabel l2 = new JLabel("LED2", JLabel.CENTER);
		l2.setForeground(Color.white);
		LEDPanel.add(l2);
		JLabel l3 = new JLabel("LED3", JLabel.CENTER);
		l3.setForeground(Color.white);
		LEDPanel.add(l3);
		JLabel l4 = new JLabel("LED4", JLabel.CENTER);
		l4.setForeground(Color.white);
		LEDPanel.add(l4);
		JLabel l5 = new JLabel("LED5", JLabel.CENTER);
		l5.setForeground(Color.white);
		LEDPanel.add(l5);
		JLabel l6 = new JLabel("LED6", JLabel.CENTER);
		l6.setForeground(Color.white);
		LEDPanel.add(l6);

		LEDPanel.setBounds(420, 270, 250, 60);
		LEDPanel.setOpaque(false);
		add(LEDPanel);

		setSize(700, 370);
		setVisible(true);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -2118047100671278658L;

	@Override
	public void onSensorValueChanged(AbstractDevice sensor, String propertyName, Object oldValue,
			Object newValue) {
		if (sensor.equals(base)) {
			if (propertyName.equals(RP6Base.MOVEMENT_COMPLETE)) {
				if ((Boolean) newValue) {
					speedLeft.setUserLedOn(true);
					speedRight.setUserLedOn(true);
				} else {
					speedLeft.setUserLedOn(false);
					speedRight.setUserLedOn(false);
				}
			} else if (propertyName.equals(RP6Base.SPEED_LEFT)) {
				speedLeft.setValue((Integer) newValue);
			} else if (propertyName.equals(RP6Base.SPEED_RIGHT)) {
				speedRight.setValue((Integer) newValue);
			} else if (propertyName.equals(RP6Base.BATTERY)) {
				battery.setValue((Double) newValue);
			} else if (propertyName.equals(RP6Base.ACS_LEFT)) {
				if ((Boolean) newValue) {
					acsLeft.setLedOn(true);
				} else {
					acsLeft.setLedOn(false);
				}
			} else if (propertyName.equals(RP6Base.ACS_RIGHT)) {
				if ((Boolean) newValue) {
					acsRight.setLedOn(true);
				} else {
					acsRight.setLedOn(false);
				}
			} else if (propertyName.equals(RP6Base.BUMPERS_LEFT)) {
				if ((Boolean) newValue) {
					bumperLeft.setLedOn(true);
				} else {
					bumperLeft.setLedOn(false);
				}
			} else if (propertyName.equals(RP6Base.BUMPERS_RIGHT)) {
				if ((Boolean) newValue) {
					bumperRight.setLedOn(true);
				} else {
					bumperRight.setLedOn(false);
				}
			} else if (propertyName.equals(RP6Base.LIGHT_LEFT)) {
				lightLeft.setValue((Integer) newValue);
			} else if (propertyName.equals(RP6Base.LIGHT_RIGHT)) {
				lightRight.setValue((Integer) newValue);
			} else if (propertyName.equals(RP6Base.RP6BASE_LEDS)) {
				if (BitOperations.isBitSet((Integer) newValue, 0)) {
					l1.setLedOn(true);
				} else {
					l1.setLedOn(false);
				}
				if (BitOperations.isBitSet((Integer) newValue, 1)) {
					l2.setLedOn(true);
				} else {
					l2.setLedOn(false);
				}
				if (BitOperations.isBitSet((Integer) newValue, 2)) {
					l3.setLedOn(true);
				} else {
					l3.setLedOn(false);
				}
				if (BitOperations.isBitSet((Integer) newValue, 3)) {
					l4.setLedOn(true);
				} else {
					l4.setLedOn(false);
				}
				if (BitOperations.isBitSet((Integer) newValue, 4)) {
					l5.setLedOn(true);
				} else {
					l5.setLedOn(false);
				}
				if (BitOperations.isBitSet((Integer) newValue, 5)) {
					l6.setLedOn(true);
				} else {
					l6.setLedOn(false);
				}
			}
		}
	}

}
